//
//  YMHealthFileChooseTypeView.m
//  YMMedical
//
//  Created by y s on 2023/7/20.
//

#import "YMHealthFileChooseTypeView.h"
#import "YMHomeChooseUserView.h"
#import "YMHomeService.h"
#import "YMMineService.h"


@interface YMHealthFileChooseTypeView ()

@property (nonatomic, strong) UIImageView *imgvUser;
@property (nonatomic, strong) UILabel *lblName;
@property (nonatomic, strong) UIImageView *imgvArrow;
@property (nonatomic, strong) UIButton *btnUser;
@property (nonatomic, strong) YMHomeChooseUserView *vChooseUser;

@property (nonatomic, strong) UIImageView *imgvItem;
@property (nonatomic, strong) UILabel *lblItem;
@property (nonatomic, strong) UIButton *btnItem;
@property (nonatomic, strong) YMHomeChooseUserView *vChooseItem;

@property (nonatomic, strong) NSArray *arrUser;
@property (nonatomic, strong) NSArray *arrItem;
@property (nonatomic, assign) NSInteger indexName;
@property (nonatomic, assign) NSInteger indexItem;

@end

@implementation YMHealthFileChooseTypeView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    
    [self addSubview:self.btnUser];
    [self.btnUser addSubview:self.imgvUser];
    [self.btnUser addSubview:self.lblName];
    [self.btnUser addSubview:self.imgvArrow];
    
    [self addSubview:self.btnItem];
    [self.btnItem addSubview:self.lblItem];
    [self.btnItem addSubview:self.imgvItem];
    
    [self.btnUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    [self.imgvUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.size.mas_equalTo([SFitUtil fitSize:22 height:22]);
        make.centerY.mas_equalTo(0);
    }];
    [self.lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imgvUser.mas_right).offset([SFitUtil fit:6]);
        make.centerY.equalTo(self.imgvUser);
    }];
    [self.imgvArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblName.mas_right).offset([SFitUtil fit:6]);
        make.centerY.equalTo(self.imgvUser);
        make.size.mas_equalTo([SFitUtil fitSize:22 height:22]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
    }];
    [self.btnItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.equalTo(self.btnUser.mas_right).offset([SFitUtil fit:15]);
        make.width.equalTo(self.btnUser);
    }];
    [self.imgvItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:22 height:22]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
    }];
    [self.lblItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.imgvItem.mas_left).offset([SFitUtil fit:-6]);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo([SFitUtil fit:15]);
    }];
}

- (void)refreshData{
    [self loadUserData];
    [self loadItemData];
}
- (void)loadUserData{
    WEAK_SELF;
    [YMMineService requestMineTesteeListDataWithPage:1 needpage:0 success:^(NSArray * _Nonnull arrData) {
        weakSelf.arrUser = arrData;
        NSMutableArray *arr1 = [[NSMutableArray alloc] init];
        for (NSDictionary *dictUser in arrData) {
            NSString *userName = [SDataUtil stringOf:dictUser[@"8pa55qzbspju5tzn"] valid:@""];
            NSString *userId = [SDataUtil stringOf:dictUser[@"id"] valid:@""];
            if (weakSelf.testeeId != nil && weakSelf.testeeId.length > 0){
                if ([userId isEqualToString:weakSelf.testeeId]){
                    weakSelf.indexName = [arrData indexOfObject:dictUser];
                }
            }
            [arr1 addObject:userName];
        }
        weakSelf.testeeId = @"";
        weakSelf.vChooseUser.arrList = arr1;
        [weakSelf changeUserData];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
}
- (void)loadItemData{
    WEAK_SELF;
    [YMHomeService requestHomeItemInforWithSegementID:self.maxIdStr success:^(NSArray * _Nonnull arrData) {
        weakSelf.arrItem = arrData;
        NSMutableArray *arr2 = [[NSMutableArray alloc] init];
        for (NSDictionary *dictItem in arrData) {
            NSString *itemName = [SDataUtil stringOf:dictItem[@"ribhfqebw2sqjovu"] valid:@""];
            NSString *minorTermId = [SDataUtil stringOf:dictItem[@"id"] valid:@""];
            if (weakSelf.idStr != nil && weakSelf.idStr.length > 0){
                if ([minorTermId isEqualToString:weakSelf.idStr]){
                    weakSelf.indexItem = [arrData indexOfObject:dictItem];
                }
            }
            [arr2 addObject:itemName];
        }
        weakSelf.idStr = @"";
        weakSelf.vChooseItem.arrList = arr2;
        [weakSelf changeItemData];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
}

- (void)userClickAction:(id)sender{
   
    [self.vChooseUser show];
    
}
- (void)itemClickAction:(id)sender{
    [self.vChooseItem show];
}


- (void)changeUserData{
    if (self.arrUser == nil || self.arrUser.count < 1){
        return;
    }
    if (self.indexName > self.arrUser.count-1){
        self.indexName = 0;
    }
    NSDictionary *dict = [SDataUtil dictOf:self.arrUser[_indexName]];
    NSString *detectionUserId = [SDataUtil stringOf:dict[@"id"] valid:@""];
    self.lblName.text =[SDataUtil stringOf:dict[@"8pa55qzbspju5tzn"] valid:@""];
    if (self.changeUserlock){
        self.changeUserlock(detectionUserId);
    }
}
- (void)changeItemData{
    if (self.arrItem == nil || self.arrItem.count < 1){
        return;
    }
    if (self.indexItem > self.arrItem.count-1){
        self.indexItem = 0;
    }
    NSDictionary *dict = [SDataUtil dictOf:self.arrItem[_indexItem]];
    NSString *minorTermId = [SDataUtil stringOf:dict[@"id"] valid:@""];
    self.lblItem.text =[SDataUtil stringOf:dict[@"ribhfqebw2sqjovu"] valid:@""];
    if (self.changeItemlock){
        self.changeItemlock(minorTermId);
    }
}

#pragma mark -- setter and getter
- (UIImageView *)imgvUser{
    if (_imgvUser == nil){
        _imgvUser = [[UIImageView alloc] init];
        _imgvUser.image = [UIImage imageNamed:@"home_nav_add_user_icon"];
    }
    return _imgvUser;
}
- (UILabel *)lblName{
    if (_lblName == nil){
        _lblName = [[UILabel alloc] init];
        _lblName.textColor = STheme.D1;
        _lblName.textAlignment = NSTextAlignmentRight;
        _lblName.font = [SFitUtil fitBoldFont:15];
    }
    return _lblName;
}
- (UIImageView *)imgvArrow{
    if (_imgvArrow == nil){
        _imgvArrow = [[UIImageView alloc] init];
        _imgvArrow.image = [UIImage imageNamed:@"home_nav_updown_arrow"];
    }
    return _imgvArrow;
}
- (UIButton *)btnUser{
    if (_btnUser == nil){
        _btnUser = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnUser addTarget:self action:@selector(userClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnUser.backgroundColor = STheme.D9;
        _btnUser.layer.cornerRadius = [SFitUtil fit:8];
        _btnUser.clipsToBounds = YES;
    }
    return _btnUser;
}
- (UILabel *)lblItem{
    if (_lblItem == nil){
        _lblItem = [[UILabel alloc] init];
        _lblItem.text = @"--";
        _lblItem.textColor = STheme.D1;
        _lblItem.font = [SFitUtil fitBoldFont:15];
    }
    return _lblItem;
}
- (UIImageView *)imgvItem{
    if (_imgvItem == nil){
        _imgvItem = [[UIImageView alloc] init];
        _imgvItem.image = [UIImage imageNamed:@"home_nav_updown_arrow"];
    }
    return _imgvItem;
}
- (UIButton *)btnItem{
    if (_btnItem == nil){
        _btnItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnItem addTarget:self action:@selector(itemClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnItem.backgroundColor = STheme.D9;
        _btnItem.layer.cornerRadius = [SFitUtil fit:8];
        _btnItem.clipsToBounds = YES;
    }
    return _btnItem;
}
- (YMHomeChooseUserView *)vChooseUser{
    if (_vChooseUser == nil){
        _vChooseUser = [[YMHomeChooseUserView alloc] init];
        _vChooseUser.yWidth = [SFitUtil fit:165];
        _vChooseUser.yPoint = CGPointMake([SFitUtil fit:15], TOP_BAR_HEIGHT+[SFitUtil fit:15+50]);
        WEAK_SELF;
        _vChooseUser.chooseUserBlock = ^(NSInteger index) {
            weakSelf.indexName = index;
            [weakSelf changeUserData];
        };
    }
    return _vChooseUser;
}
- (YMHomeChooseUserView *)vChooseItem{
    if (_vChooseItem == nil){
        _vChooseItem = [[YMHomeChooseUserView alloc] init];
        _vChooseItem.yWidth = [SFitUtil fit:165];
        _vChooseItem.yPoint = CGPointMake([SFitUtil fit:195], TOP_BAR_HEIGHT+[SFitUtil fit:15+50]);
        WEAK_SELF;
        _vChooseItem.chooseUserBlock = ^(NSInteger index) {
            weakSelf.indexItem = index;
            [weakSelf changeItemData];
        };
    }
    return _vChooseItem;
}

@end
